<template>
  <div>
    <div class="search-box">
      用户账号：
      <el-input v-model="searchAccount" placeholder></el-input>用户姓名：
      <el-input v-model="searchName" placeholder></el-input>联系方式：
      <el-input v-model="searchPhone" placeholder></el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
      <el-button type="primary" icon="el-icon-plus" @click="showAddDialog">新增用户</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userAccount" label="用户账号" width="180"></el-table-column>
      <el-table-column prop="userName" label="用户名" width="180"></el-table-column>
      <el-table-column prop="userPassword" label="用户密码" width="180"></el-table-column>
      <el-table-column prop="vip" label="是否VIP" width="180"></el-table-column>
      <el-table-column prop="userMoney" label="用户余额" width="180"></el-table-column>
      <el-table-column prop="userPhone" label="用户电话" width="180"></el-table-column>
      <el-table-column prop="userIdcard" label="用户身份证" width="180"></el-table-column>
      <el-table-column prop="userSex" label="用户性别" width="180"></el-table-column>
      <el-table-column prop="status" label="状态" width="180"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">修改</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[4, 8, 10, 12]"
      :page-size="4"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 修改表单的对话框 -->
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" ref="editForm">
        <el-form-item label="用户账号" prop="userAccount">
          <el-input v-model="editForm.userAccount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="editForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="userPassword">
          <el-input v-model="editForm.userPassword" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="是否VIP" prop="vip">
          <el-switch v-model="editForm.vip"></el-switch>
        </el-form-item>
        <el-form-item label="用户余额" prop="userMoney">
          <el-input-number v-model="editForm.userMoney" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="用户电话" prop="userPhone">
          <el-input v-model="editForm.userPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户身份证" prop="userIdcard">
          <el-input v-model="editForm.userIdcard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="userSex">
          <el-radio-group v-model="editForm.userSex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="editForm.status" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker v-model="editForm.createTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>

    <!-- 新增用户的对话框 -->
    <el-dialog title="新增用户" :visible.sync="addDialogFormVisible">
      <el-form :model="addForm" ref="addForm">
        <el-form-item label="用户账号" prop="userAccount">
          <el-input v-model="addForm.userAccount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="addForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="userPassword">
          <el-input v-model="addForm.userPassword" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="是否VIP" prop="vip">
          <el-switch v-model="addForm.vip"></el-switch>
        </el-form-item>
        <el-form-item label="用户余额" prop="userMoney">
          <el-input-number v-model="addForm.userMoney" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="用户电话" prop="userPhone">
          <el-input v-model="addForm.userPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户身份证" prop="userIdcard">
          <el-input v-model="addForm.userIdcard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="userSex">
          <el-radio-group v-model="addForm.userSex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitAddForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      pageSize: 4,
      searchName: "",
      searchAccount: "",
      searchPhone: "",
      editForm: {},
      dialogFormVisible: false,
      addDialogFormVisible: false,
      addForm: {
        userAccount: "",
        userName: "",
        userPassword: "",
        vip: false,
        userMoney: 0.0,
        userPhone: "",
        userIdcard: "",
        userSex: "男"
      }
    };
  },
  methods: {
    feachData() {
      this.$axios
        .get("/user/getUsers", {
          params: {
            account: this.searchAccount,
            userName: this.searchName,
            userPhone: this.searchPhone,
            currpage: this.currentPage,
            pagesize: this.pageSize
          }
        })
        .then(res => {
          console.log(res);
          this.tableData = res.data; // 假设返回的数据是用户列表
          this.total = res.total;
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.feachData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.feachData();
    },
    search() {
      this.feachData();
    },
    reset() {
      this.searchAccount = "";
      this.searchName = "";
      this.searchPhone = "";
      this.feachData();
    },
    handleEdit(row) {
      this.editForm = { ...row }; // 将当前行的数据赋值给编辑表单
      this.dialogFormVisible = true; // 显示对话框
    },
    handleDelete(row) {
      // 删除操作前的确认
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 用户确认删除
          this.$axios.post("/user/delUser", row).then(res => {
            console.log(res);
            this.dialogFormVisible = false; // 关闭对话框
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }

            this.fetchData(); // 重新加载数据
          });
        })
        .catch(() => {
          // 用户取消删除
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    submitForm() {
      console.log(this.editForm);
      this.$axios.post("/user/updateUser", this.editForm).then(res => {
        console.log(res);
        this.dialogFormVisible = false; // 关闭对话框
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg
          });
        }
        this.feachData(); // 重新加载数据
      });
    },
    submitAddForm() {
      console.log(this.addForm);
      this.$axios.post("/user/addUser", this.addForm).then(res => {
        console.log(res);
        this.addDialogFormVisible = false; // 关闭对话框
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg
          });
          this.feachData(); // 重新加载数据
        } else {
          this.$message({
            type: "error",
            message: res.msg
          });
        }
      });
    },
    showAddDialog() {
      this.addForm = {
        userAccount: "",
        userName: "",
        userPassword: "",
        vip: false,
        userMoney: 0.0,
        userPhone: "",
        userIdcard: "",
        userSex: "男",
        status: ""
      };
      this.addDialogFormVisible = true; // 显示新增用户对话框
    }
  },
  mounted() {
    this.feachData();
  }
};
</script>

<style scoped>
.search-box {
  display: flex;
  align-items: center;
  margin: 20px;
}

.search-box .el-input {
  width: 200px;
  margin-right: 10px;
}

.search-box .el-button {
  margin-left: 10px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.el-dialog .el-form {
  width: 90%;
  margin: 0 auto;
}

.el-dialog .el-form-item {
  margin-bottom: 22px;
}

.el-dialog .el-input,
.el-dialog .el-date-editor,
.el-dialog .el-radio-group {
  width: 100%;
}

.dialog-footer {
  text-align: right;
}
</style>

